﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace XHCTMS.VIEW
{
    public partial class Department : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            View();
        }        

        /// <summary>
        /// 增加部门
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtDepName.Text.Trim()) || string.IsNullOrEmpty(txtDesc.Text.Trim()))
            {
                ScriptManager.RegisterStartupScript
                (this.btnAdd, this.GetType(), 
                "fd", "$(function(){$('<div title=\"提示\">不能有空</div>').dialog({buttons: {\"确认\": function () {$(this).dialog('close');}}});})", true);
            }
            else
            {
                XHCTMS.MODEL.Department dep = new MODEL.Department();                
                dep.DepName = txtDepName.Text;
                dep.DepDesc = txtDesc.Text;

                if (new XHCTMS.BLL.Department().Add(dep) == -1)
                {
                    ScriptManager.RegisterStartupScript
                    (this.btnAdd, this.GetType(),
                    "fd", "$(function(){$('<div title=\"提示\">系统繁忙</div>').dialog({buttons: {\"确认\": function () {$(this).dialog('close');}}});})", true);
                    View();
                }
                else
                {
                    ScriptManager.RegisterStartupScript
                    (this.btnAdd, this.GetType(),
                    "fd", "$(function(){$('<div title=\"提示\">增加成功</div>').dialog({buttons: {\"确认\": function () {$(this).dialog('close');}}});})", true);
                    View();
                }
            }
            Clear();
        }
        
        /// <summary>
        /// 查询部门信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {

            string depName = txtDepName.Text;
            if (new XHCTMS.BLL.Department().GetDepartment(depName) == null)
            {
                ScriptManager.RegisterStartupScript
                    (this.btnAdd, this.GetType(),
                    "fd", "$(function(){$('<div title=\"提示\">信息不存在</div>').dialog({buttons: {\"确认\": function () {$(this).dialog('close');}}});})", true);
                View();
            }
            else
            {
                if (string.IsNullOrEmpty(txtDepName.Text.Trim()))
                {
                    bool rbtn = !(rbtnNo.Checked);
                    int pageIndex=1, pageSize=100;
                    if (rbtn == false)
                    {
                        this.rperData.DataSource = new XHCTMS.BLL.Department().GetDepartment(pageIndex, pageSize, rbtn);
                        this.rperData.DataBind();
                    }
                }
                //Boolean.TryParse(rbtnNo.Text, out rbtn);
                else
                {
                    this.rperData.DataSource = new XHCTMS.BLL.Department().GetDepartment(depName);
                    this.rperData.DataBind();                    
                }
            }
            Clear();
        }

        /// <summary>
        /// 更新部门信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAlter_Click(object sender, EventArgs e)
        {
            View();
        }

        /// <summary>
        /// 修改部门信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUpdate_Click(object sender, EventArgs e)
        {            
            XHCTMS.MODEL.Department dep = new MODEL.Department();
            dep.DepName = txtDepName.Text;
            dep.DepDesc = txtDesc.Text;
            if (new XHCTMS.BLL.Department().GetId(txtDepName.Text) == null)
            {
                ScriptManager.RegisterStartupScript
                (this.btnUpdate, this.GetType(),
                "fd", "$(function(){$('<div title=\"提示\">信息不存在</div>').dialog({buttons: {\"确认\": function () {$(this).dialog('close');}}});})", true);
            }
            else
            {
                txtDepId.Text = new XHCTMS.BLL.Department().GetId(txtDepName.Text);
                dep.DepId = int.Parse(txtDepId.Text);
                if (new XHCTMS.BLL.Department().Update(dep) == -1)
                {
                    ScriptManager.RegisterStartupScript
                    (this.btnUpdate, this.GetType(),
                    "fd", "$(function(){$('<div title=\"提示\">系统繁忙</div>').dialog({buttons: {\"确认\": function () {$(this).dialog('close');}}});})", true);
                    View();
                }
                else
                {
                    ScriptManager.RegisterStartupScript
                    (this.btnUpdate, this.GetType(),
                    "fd", "$(function(){$('<div title=\"提示\">修改成功</div>').dialog({buttons: {\"确认\": function () {$(this).dialog('close');}}});})", true);
                    View();
                }
            }
            Clear();
        } 

        /// <summary>
        /// 删除部门信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param> 
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (new XHCTMS.BLL.Department().Delete(int.Parse(txtDepId.Text)) == -1)
                {
                    ScriptManager.RegisterStartupScript
                    (this.btnUpdate, this.GetType(),
                    "fd", "$(function(){$('<div title=\"提示\">删除成功</div>').dialog({buttons: {\"确认\": function () {$(this).dialog('close');}}});})", true);
                    View();
                }
                else
                {                    
                    View();
                }
            }
            catch
            {                
            }
        }
        
   
        public void View()
        {
              XHCTMS.BLL.Department dep = new BLL.Department();
              this.rperData.DataSource = dep.GetDepartment();
              this.rperData.DataBind();
        }

        public void Clear()
        {
            txtDepName.Text = string.Empty;
            txtDepId.Text = string.Empty;
            txtDesc.Text = string.Empty;
            rbtnNo.Checked = false;
        }

    }
}